import { Controller, Get, Post, Body, Param, Put, Delete, ParseUUIDPipe, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { AuditLog, Permissions, PermissionsGuard } from '@nbase/nest-mods';
import { AUDIT_LOG_OPERATION_TYPE, type IUser, type IPaginationResponse } from '@nbase/shared';
import { UserService } from './user.service';
import { PostService } from '../post/post.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { QueryUserDto } from './dto/query-user.dto';
import { AssignRolesDto } from './dto/assign-roles.dto';
import { AssignPostsDto } from '../post/dto/assign-posts.dto';

@ApiTags('system/user')
@ApiBearerAuth('JWT-auth')
@Controller('system/user')
@UseGuards(PermissionsGuard)
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly postService: PostService
  ) {}

  @ApiOperation({ summary: '获取用户列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:user:query')
  @Get()
  findAll(@Query() query: QueryUserDto): Promise<IPaginationResponse<IUser>> {
    return this.userService.paginateUsers(query);
  }

  @ApiOperation({ summary: '获取用户详情' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @Permissions('system:user:query')
  @Get(':id')
  findOne(@Param('id', ParseUUIDPipe) id: string): Promise<IUser> {
    return this.userService.getUserById(id);
  }

  @AuditLog({
    module: '用户管理',
    operationType: AUDIT_LOG_OPERATION_TYPE.CREATE,
    description: '创建用户',
  })
  @ApiOperation({ summary: '创建用户' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @Permissions('system:user:create')
  @Post()
  create(@Body() createUserDto: CreateUserDto): Promise<IUser> {
    return this.userService.createUser(createUserDto);
  }

  @AuditLog({
    module: '用户管理',
    operationType: AUDIT_LOG_OPERATION_TYPE.UPDATE,
    description: '更新用户',
  })
  @ApiOperation({ summary: '更新用户' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @Permissions('system:user:update')
  @Put(':id')
  update(@Param('id', ParseUUIDPipe) id: string, @Body() updateUserDto: UpdateUserDto): Promise<IUser> {
    return this.userService.updateUser(id, updateUserDto);
  }

  @AuditLog({
    module: '用户管理',
    operationType: AUDIT_LOG_OPERATION_TYPE.UPDATE,
    description: '重置用户密码',
  })
  @ApiOperation({ summary: '重置用户密码为默认值 123456' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '重置成功' })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @Permissions('system:user:update')
  @Post(':id/reset-password')
  async resetPassword(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.userService.resetPassword(id);
  }


  @AuditLog({
    module: '用户管理',
    operationType: AUDIT_LOG_OPERATION_TYPE.DELETE,
    description: '删除用户',
  })
  @ApiOperation({ summary: '删除用户' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '用户不存在' })
  @Permissions('system:user:delete')
  @Delete(':id')
  async remove(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.userService.deleteUser(id);
  }

  @ApiOperation({ summary: '为用户分配角色' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '分配成功' })
  @Permissions('system:user:update')
  @Post(':id/roles')
  async assignRoles(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() assignRolesDto: AssignRolesDto
  ): Promise<void> {
    await this.userService.assignRoles(id, assignRolesDto);
  }

  @ApiOperation({ summary: '获取用户的角色列表' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:user:query')
  @Get(':id/roles')
  getUserRoles(@Param('id', ParseUUIDPipe) id: string): Promise<string[]> {
    return this.userService.getUserRoles(id);
  }

  @ApiOperation({ summary: '为用户分配岗位' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '分配成功' })
  @Permissions('system:user:update')
  @Post(':id/posts')
  async assignPosts(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() assignPostsDto: AssignPostsDto
  ): Promise<void> {
    await this.postService.assignPostsToUser(id, assignPostsDto);
  }

  @ApiOperation({ summary: '获取用户的岗位列表' })
  @ApiParam({ name: 'id', description: '用户ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:user:query')
  @Get(':id/posts')
  getUserPosts(@Param('id', ParseUUIDPipe) id: string): Promise<string[]> {
    return this.postService.getUserPosts(id);
  }
}

